package com.goldsprite.littlegame.screens.games.phase_1;

import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.goldsprite.gdxcore.screens.GScreen;
import com.goldsprite.littlegame.assets.GlobalAssets;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Window;

public class P1ExampleSelectScreen extends GScreen{
	private Skin skin;
	private Viewport viewport;
	private OrthographicCamera cam;
	private Stage stage;
	
	private List<ScreenTurn> screenList;
	

	@Override
	public void create() {
		skin = GlobalAssets.getInstance().editorSkin;

		viewport = getViewport();
		cam = (OrthographicCamera)viewport.getCamera();
		stage = new Stage(viewport);
		getImp().addProcessor(stage);
		
		createScreenList();
	}

	private void createScreenList() {
		screenList = new List<>(skin);
		//screenList.setFillParent(true);
		
		int cLen = 2;
		Object[] turnDatas = {
			"P1主游戏", Phase_1_Game.class, 
			"Box2D演示", Box2DExample.class
		};
		ScreenTurn[] turns = new ScreenTurn[turnDatas.length/cLen];
		for(int i=0;i<turnDatas.length;i+=cLen){
			ScreenTurn turn = new ScreenTurn();
			turn.title = turnDatas[i].toString();
			turn.turnScreen = (Class<? extends GScreen>)turnDatas[i+1];
			turns[i/cLen] = turn;
		}
		screenList.setItems(turns);
		screenList.addListener(new ChangeListener(){
			public void changed(ChangeListener.ChangeEvent ev, Actor a){
				int selection = screenList.getSelectedIndex();
				if(selection < 0 || selection > turns.length-1) return;
				ScreenTurn turn = turns[selection];
				getScreenManager().setCurScreen(turn.turnScreen, true);
				screenList.setSelectedIndex(-1);
			}
		});
		
		screenList.setSelectedIndex(-1);
		
		Window window = new Window("P1演示选择界面", skin);
		window.setFillParent(true);
		window.add(screenList).expand().fill();
		
		stage.addActor(window);
	}
	

	@Override
	public void render(float delta) {
		super.render(delta);
		
		stage.act(delta);
		stage.draw();
	}
	
	
	public static class ScreenTurn{
		public String title="";
		public Class<? extends GScreen> turnScreen;

		@Override
		public String toString() {
			return title;
		}
	}

}
